Collisions = {}
CO = {}

function Collisions.newObject (cl, scr, x, y, x2, y2, mode)
  CO[cl] = {}
  
  CO[cl].x = x
  CO[cl].y = y
  CO[cl].x2 = x2
  CO[cl].y2 = y2
  
  CO[cl].scr = scr
  
  CO[cl].mode = mode
  
  CO[cl].isInCollision = false
  CO[cl].inCollisionWith = 0
  CO[cl].collisionMode = ""
end

function Collisions.drawObject (cl, two)
  if CO[cl].mode == "Rect" then
    screen.drawRect (CO[cl].scr, CO[cl].x, CO[cl].y, CO[cl].x2, CO[cl].y2, two)
  elseif CO[cl].mode == "FillRect" then
    screen.drawFillRect (CO[cl].scr, CO[cl].x, CO[cl].y, CO[cl].x2, CO[cl].y2, two)
  elseif CO[cl].mode == "GradientRect" then
    screen.drawGradientRect (CO[cl].scr, CO[cl].x, CO[cl].y, CO[cl].x2, CO[cl].y2, two)
  elseif CO[cl].mode == "Image" then
    screen.blit (CO[cl].scr, CO[cl].x, CO[cl].y, two)
  end
end

function Collisions.initCollision (one, two)
 if CO[one].x2 == CO[two].x and CO[one].y <= CO[two].y2 and CO[one].y >= CO[two].y then
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "right"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "left"
  elseif CO[one].x == CO[two].x2 and CO[one].y <= CO[two].y2 and CO[one].y2 >= CO[two].y then
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "left"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "right"
  elseif CO[one].y == CO[two].y2 and CO[one].x >= CO[two].x and CO[one].x2 <= CO[two].x2 then 
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "up"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "down"
  elseif CO[one].y2 == CO[two].y and CO[one].x >= CO[two].x and CO[one].x2 <= CO[two].x2 then
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "down"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "up"
  else
    CO[one].isInCollision = false
    CO[one].inCollisionWith = 0
    CO[one].collisionMode = ""
    CO[two].isInCollison = false
    CO[two].inCollisionWith = 0
    CO[two].collisionMode = ""
  end
	
  if CO[one].x2 == CO[two].x and CO[one].y2 >= CO[two].y and CO[one].y <= CO[two].y2 then
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "right"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "left"
	end
	
	if CO[one].y == CO[two].y2 and CO[one].x2 >= CO[two].x and CO[one].x <= CO[two].x2 then 
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "up"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "down"
	end
	
	if CO[one].y2 == CO[two].y and CO[one].x2 >= CO[two].x and CO[one].x <= CO[two].x2 then
    CO[one].isInCollision = true
    CO[one].inCollisionWith = two
    CO[one].collisionMode = "down"
    CO[two].isInCollison = true
    CO[two].inCollisionWith = one
    CO[two].collisionMode = "up"
	end
end

function Collisions.isInCollision (cl)
  return CO[cl].isInCollision
end

function Collisions.isInCollisionWith (one, two)
  local ret = false
  
  if CO[one].inCollisionWith == two then
    ret = true
  else
    ret = false
  end
  
  return ret
end

function Collisions.update (cl, x, y, x2, y2)
  CO[cl].x = x
  CO[cl].y = y
  CO[cl].x2 = x2
  CO[cl].y2 = y2
end

function Collisions.mode (cl)
  return CO[cl].collisionMode
end

function Collisions.destroyObject (cl)
  _G[cl] = nil
end
